<template>
    <div>
        <el-divider content-position="left">您具有一定的操作权限</el-divider>
    </div>

    <div>
        <el-divider>
            <span id="spanlittlepart">查询</span>
        </el-divider>
    </div>
    <!-- 查询 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="用户名" style="width: 130px;">
            <el-input v-model="checkformInline.username" placeholder="username"></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" style="width: 150px;">
            <el-input v-model="checkformInline.nickname" placeholder="nickname"></el-input>
        </el-form-item>
        <el-form-item label="手机号" style="width: 120px;">
            <el-input v-model="checkformInline.mobile" placeholder="mobile"></el-input>
        </el-form-item>
        <el-form-item label="注册时间" style="width: 200px;">
            <el-input v-model="checkformInline.create_time" placeholder="格式如‘2023-5-14’"></el-input>
        </el-form-item>
        <el-form-item style="width: 200px;">
            <el-button @click="search" type="primary" plain style="width: 200px;">查询</el-button>
        </el-form-item>
    </el-form>

    <el-divider>
        <el-icon><star-filled /></el-icon>
    </el-divider>

    <!-- 展示表格 -->
    <el-table :data="tableData" style="width: 100%" highlight-current-row="true" :scrollbar-always-on="true">
        <el-table-column prop="userid" label="用户id" width="" />
        <el-table-column prop="username" label="用户名" width="120" />
        <el-table-column prop="nickname" label="用户昵称" width="150" />
        <el-table-column prop="mobile" label="手机号" width="150" />
        <el-table-column prop="create_time" :formatter="Formatter" label="注册时间" width="120" />
        <el-table-column label="用户头像" width="180">
            <template #default="scope">
                <el-image style="width: 100px; height: 100px; padding-left: -30px;margin-left: -30px; overflow: visible;"
                    :src="baseURL + tableData[scope.$index].avatar" :preview-src-list="[baseURL + tableData[scope.$index].avatar]" :preview-teleported="true"
                    :initial-index="scope.$index">
                </el-image>
            </template>
        </el-table-column>
        <el-table-column prop="signature" label="个性签名" width="280" />
        <el-table-column fixed="right" label="操作" width="100">
            <template #default="scope">
                <el-button link type="primary" size="small" style="margin-left: 0px;"
                    @click.prevent="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>


    <el-pagination background layout="prev, pager, next" :current-page="page" @update:current-page="handleCurrentChange"
        :total="total" :page-size="size" style="bottom: 0; text-align: center;margin-left: 45%;margin-top: 5px;" />
</template>
<script setup>
import {
    Guide,
    PictureFilled,
    User,
    HomeFilled,
    ChatLineSquare,
    Apple,
    Document,
    List,
    Tools,
    ShoppingCart,
    CollectionTag,
    Finished,
    InfoFilled,
    Cherry,
    Goods,
    Sell,
    ShoppingCartFull,
    Van,
    Sunrise,
    StarFilled,
    EditPen,
    Edit,
    Promotion,
    Grape,
    Pear,
} from '@element-plus/icons-vue';
import dayjs from 'dayjs';
import { ElMessage, ElMessageBox } from 'element-plus';
import axios from 'axios';
const formLabelWidth = '140px';

</script>
<script>

export default {
    data() {
        return {
            baseURL: 'http://localhost:8080',
            formInline: {
                userid: 0,
                username: '',
                nickname: '',
                password: '',
                mobile: '',
                create_time: '格式如‘2023-5-14’',
                avatar: '',
                signature: '',
                birthday: '',
                city: '',
                id: 0,
                potid: 0
            },
            checkformInline: {
                userid: 0,
                username: '',
                nickname: '',
                password: '',
                mobile: '',
                create_time: '',
                avatar: '',
                signature: '',
                birthday: '',
                city: '',
                id: 0,
                potid: 0
            },
            dialogFormVisible: false,
            showSearch: false,
            tableData: [],
            reallyTable: [],
            tableDataStore: [],
            tableimgList: [],
            selData: [],
            tableimg: '',
            user: {
                userid: 0,
                username: '',
                nickname: '',
                password: '',
                mobile: '',
                create_time: 0,
                avatar: '',
                signature: '',
                birthday: '',
                city: '',
                id: 0,
                potid: 0
            },
            form: {
                userid: 0,
                username: '',
                nickname: '',
                password: '',
                mobile: '',
                create_time: 0,
                avatar: '',
                signature: '',
                birthday: '',
                city: '',
                id: 0,
                potid: 0
            },
            searchForm: [],
            searchtime: [],
            year: 0,
            month: 0,
            daynum: 0,
            url: require("../../public/l.jpg"),
            page: 1, // 当前页码
            total: 0, // 总条数
            size: 10, // 每页的数据条数
        }

    },
    methods: {
        init(){
            axios({
            method: 'post',
            url: 'http://localhost:8080/users/getuser',
            withCredentials: true,
        }).then(res => {
            this.tableData = res.data.data;
            this.tableData = res.data.data.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            this.total = res.data.data.length;
            this.selData = res.data.data;
            this.tableDataStore = res.data.data;
            console.log(res.data.data);
            for (var i = 0; i < res.data.data.length; i++) {
                this.tableimgList.push(res.data.data[i].avatar);
            }
            //console.log("tableimglist:" + this.tableimgList);
            this.tableimg = this.tableimgList[0];
            //console.log("tableimg：-------->" + this.tableimg);
        })
        },
        //删除表格行
        handleDelete(index, row) {
            console.log(index, row);
            console.log(row.userid);//可以获取到
            ElMessageBox.confirm(
                '确认删除本项吗？删除后无法找回:-0',
                'Warning',
                {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            )
                .then(() => {
                    axios({
                        method: 'post',
                        url: 'http://localhost:8080/users/deluserbackend',
                        withCredentials: true,
                        data: {
                            userid: row.userid,
                        },
                    }).then(res => {
                        this.tableData = res.data.data;
                        this.tableData = res.data.data.slice(
                            (this.page - 1) * this.size,
                            this.page * this.size
                        );
                        this.total = res.data.data.length;
                        this.selData = res.data.data;
                        this.tableDataStore = res.data.data;
                        console.log(res.data.data);
                        for (var i = 0; i < res.data.data.length; i++) {
                            this.tableimgList.push(res.data.data[i].avatar);
                        }
                        //console.log("tableimglist:" + this.tableimgList);
                        this.tableimg = this.tableimgList[0];
                        //console.log("tableimg：-------->" + this.tableimg);
                        ElMessage({
                            type: 'success',
                            message: '删除成功',
                        })
                    })
                })
                .catch(() => {
                    ElMessage({
                        type: 'info',
                        message: '取消删除操作',
                    })
                })
        },
        formatDate(date) {
            var y = new Date(date).getFullYear();
            var m = new Date(date).getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            var d = new Date(date).getDate();
            d = d < 10 ? ('0' + d) : d;
            return y + '-' + m + '-' + d;
        },
        Formatter(row, column) {
            if (row.create_time) {
                //console.log(new Date(row.create_time));
                this.day = new Date(row.create_time);
            }
            //console.log(this.formatDate(this.day));
            return this.formatDate(row.create_time);
        },
        backtosimpleformatDate(date) {
            var y = new Date(date).getFullYear();
            this.year = y;
            var m = new Date(date).getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            this.month = m;
            var d = new Date(date).getDate();
            d = d < 10 ? ('0' + d) : d;
            this.daynum = d;
            return y + '-' + m + '-' + d;
        },
        backtosimpleTime(date) {
            this.searchtime = this.checkformInline.create_time.split('-');
            this.backtosimpleformatDate(date);
            if (this.searchtime[0] == this.year && this.searchtime[1] == this.month && this.searchtime[2] == this.daynum) {
                return 'get';
            }
            else {
                return 'fail';
            }
        },
        search() {
            let arr = []
            for (let i = 0; i < this.tableDataStore.length; i++) {
                this.tableDataStore[i].create_time = this.formatDate(this.tableDataStore[i].create_time);
            }
            for (let i = 0; i < this.tableDataStore.length; i++) {
                if (this.tableDataStore[i].username.includes(this.checkformInline.username) &&
                    this.tableDataStore[i].nickname.includes(this.checkformInline.nickname) &&
                    this.tableDataStore[i].mobile.includes(this.checkformInline.mobile) &&
                    this.tableDataStore[i].create_time.includes(this.checkformInline.create_time)) {
                    arr.push(this.tableDataStore[i])
                }
            }
            console.log(arr);
            this.tableData = arr.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            this.selData = arr;
            this.total = arr.length;
        },
        async handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.page = val
            this.tableData = this.selData.slice(
                (this.page - 1) * this.size,
                this.page * this.size
            );
            // this.currentPage = val;
            const data = { 'page': val }
            // await this.onload(data)
        },

    },
    mounted() {
        //页面加载完成后，将数据库内的内容赋给定义的data变量
        this.init()
    }
}
</script>
<style scoped>
#sidepart {
    width: 100%;
    position: absolute;
    top: 5px;
    font-family: '华文中宋';
}

#mainpart {
    width: 80%;
    position: absolute;
    top: 20px;
    left: 260px;
    font-family: '华文中宋';
}

#spanpart {
    font-size: 30px;
    font-family: '华文中宋';
}

/* dialog */
.el-button--text {
    margin-right: 15px;
}

.el-select {
    width: 300px;
}

.el-input {
    width: 300px;
}

.dialog-footer button:first-child {
    margin-right: 10px;
}

#spanlittlepart {
    font-size: 15px;
    color: cadetblue;
    font-family: '华文中宋';
}
</style>